package com.some.jobdescapp.widget;

import android.support.v4.util.SparseArrayCompat;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.some.jobdescapp.MyApplication;
import com.some.jobdescapp.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author RANDY.ZHANG
 * @version 1.0
 * @company:
 * @date 2017/9/15 18:20
 */

public class PhotoAdapter extends PagerAdapter{
    public static final int MAX_SIZE = 20000;
    private List<String> avatars = new ArrayList<>();
    private List<Integer> ids = new ArrayList<>();
    private boolean supportLoop;

    private List<View> cacheViews = new ArrayList<View>();
    private SparseArrayCompat<View> sparseArrayCompat = new SparseArrayCompat<View>();

    public int getRealCount() {
        return Math.max(avatars.size(), ids.size());
    }

    @Override
    public int getCount() {
        if (supportLoop && (avatars.size() > 1 || ids.size() > 0)) {
            return MAX_SIZE;
        } else {
            return Math.max(avatars.size(), ids.size());
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = getCacheView(position);
        ImageView imageView = (ImageView) v.findViewById(R.id.avatar_view);
        final int realPosition = position % getRealCount();
        if (ids.size() > 0) {
            imageView.setImageResource(ids.get(realPosition));
        } else {
            //TODO..
        }
//        Object cuPath = avatars.get(realPosition);
//        String pathUrl = String.valueOf(cuPath);
//        if (pathUrl.startsWith("http")) {
////            ImageLoaderUtil.loadImage(pathUrl, Configs.IMAGE_TYPE_URL, imageView);
//        } else {
////            ImageLoaderUtil.loadImage(pathUrl, Configs.IMAGE_TYPE_ALBUM_250x250, imageView);
//        }

        container.addView(v);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onKliaoProfilePhotoPagerClickListener != null) {
                    onKliaoProfilePhotoPagerClickListener.onPhotoClick(realPosition);
                }
            }
        });
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = sparseArrayCompat.get(position, null);
        if (view != null) {
            sparseArrayCompat.remove(position);
            container.removeView(view);
            cacheViews.add(view);
        }
    }

    private View getCacheView(int position){
        if (cacheViews.isEmpty()) {
            View view = LayoutInflater.from(MyApplication.application).inflate(R.layout.listitem_banner_view, null);
            sparseArrayCompat.append(position, view);
            return view;
        } else {
            return cacheViews.remove(0);
        }
    }

    public void reFreshAll(List<String> paths, boolean supportLoop) {
        avatars = paths;
        this.supportLoop = supportLoop;
        notifyDataSetChanged();
    }

    public void reFreshAllByIds(List<Integer> ids, boolean supportLoop) {
        this.ids = ids;
        avatars.clear();
        this.supportLoop = supportLoop;
        notifyDataSetChanged();
    }

    private OnKliaoProfilePhotoPagerClickListener onKliaoProfilePhotoPagerClickListener;

    public void setOnKliaoProfilePhotoPagerClickListener(OnKliaoProfilePhotoPagerClickListener onKliaoProfilePhotoPagerClickListener) {
        this.onKliaoProfilePhotoPagerClickListener = onKliaoProfilePhotoPagerClickListener;
    }

    public interface OnKliaoProfilePhotoPagerClickListener {
        void onPhotoClick(int position);
    }
}
